<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="标题">
                <a-input
                  v-model="queryParam.title"
                  placeholder="请输入标题"
                  allow-clear
                  @keyup.enter.native="handleQuery"
                />
              </a-form-item>
            </a-col>
            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-linshi-manage-add-form
        v-if="showAddModal"
        ref="bizLinshiManageAddForm"
        :statusOptions="statusOptions"
        :deptOptions="deptOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-linshi-manage-edit-form
        v-if="showEditModal"
        ref="bizLinshiManageEditForm"
        :statusOptions="statusOptions"
        :deptOptions="deptOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <biz-linshi-manage-details-form
        v-if="showEditModal"
        ref="bizLinshiManageEditForm"
        :statusOptions="statusOptions"
        :deptOptions="deptOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        title="临时督办"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        tableKey="base-bizLinshiManage-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizLinshiManageList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
        <div class="table-operations" slot="button">
          <a-button
            type="primary"
            @click="handleAdd"
            v-hasPermi="['biz:bizLinshiManage:add']"
          >
            <a-icon type="plus" />新增
          </a-button>
          <a-button
            type=""
            @click="handleExport"
            v-hasPermi="['biz:bizLinshiManage:export']"
          >
            <a-icon type="download" />导出
          </a-button>
          <a-button
            type="danger"
            v-if="!multiple"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:bizLinshiManage:remove']"
          >
            <a-icon type="delete" />删除
          </a-button>
        </div>

        <span slot="status" slot-scope="{ text, record }">
          <a-tag v-if="text == '0'">
            {{ statusFormat(record) }}
          </a-tag>
          <a-tag v-if="text == '1'" color="#2db7f5">
            {{ statusFormat(record) }}
          </a-tag>

          <a-tag v-if="text == '2'" color="#87d068">
            {{ statusFormat(record) }}
          </a-tag>
        </span>

        <span slot="operation" slot-scope="{ text, record }">
          <a
            @click="onfabuBizLinshiManage(record)"
            v-if="record.status == '0'"
            v-hasPermi="['biz:bizLinshiManage:edit']"
          >
            发布
          </a>
          <a-divider
            type="vertical"
            v-hasPermi="['biz:bizLinshiManage:remove']"
            v-if="record.status == '0'"
          />
          <a
            @click="handleUpdate(record, false)"
            v-hasPermi="['biz:bizLinshiManage:edit']"
            v-if="record.status == '0'"
          >
            修改
          </a>
          <a-divider
            type="vertical"
            v-hasPermi="['biz:bizLinshiManage:remove']"
            v-if="record.status == '0'"
          />
          <a
            @click="handleDelete(record)"
            v-hasPermi="['biz:bizLinshiManage:remove']"
            v-if="record.status == '0'"
          >
            删除
          </a>
          <a
            @click="handleUpdate(record, true)"
            v-hasPermi="['biz:bizLinshiManage:edit']"
            v-if="record.status != '0'"
          >
            详情
          </a>
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>

        <span slot="deptcounts" slot-scope="{ text }">
          {{ JSON.parse(text).length }}
        </span>
        <span slot="daicounts" slot-scope="{ text, record }">
          {{ JSON.parse(record.deptNames).length - record.yhuizhiCounts }}
        </span>

        <span slot="deptNames" slot-scope="{ text }">
          <a-tooltip>
            <template slot="title">
              <span v-for="(item, index) in JSON.parse(text)" :key="index">
                {{ item }}
                <span v-if="index < JSON.parse(text).length - 1">/</span>
              </span>
            </template>

            <span v-for="(item, index) in JSON.parse(text)" :key="index">
              {{ item }}
              <span v-if="index < JSON.parse(text).length - 1">/</span>
            </span>
          </a-tooltip>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import {
  listBizLinshiManage,
  delBizLinshiManage,
  exportBizLinshiManage,
  fabuBizLinshiManage,
} from "@/api/biz/bizLinshiManage";
import { listDeptTree } from "@/api/system/dept";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizLinshiManageAddForm from "@/views/biz/linshimanage/modules/BizLinshiManageAddForm";
import BizLinshiManageEditForm from "@/views/biz/linshimanage/modules/BizLinshiManageEditForm";
import BizLinshiManageDetailsForm from "@/views/biz/linshimanage/modules/BizLinshiManageDetailsForm";
export default {
  name: "BizLinshiManage",
  components: {
    AdvanceTable,
    BizLinshiManageAddForm,
    BizLinshiManageEditForm,
    BizLinshiManageDetailsForm,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 临时督办表格数据
      bizLinshiManageList: [],
      // 状态字典
      statusOptions: [],
      // 部门
      deptOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
      },
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "5%",
          scopedSlots: { customRender: "rowindex" },
        },

        {
          title: "标题",
          dataIndex: "title",
          ellipsis: true,
          align: "center",
          width: "30%",
        },
        {
          title: "督办企业",
          dataIndex: "deptNames",
          ellipsis: true,
          align: "center",
          width: "30%",
          scopedSlots: { customRender: "deptNames" },
        },
        {
          title: "督办企业数",
          dataIndex: "deptNames",
          ellipsis: true,
          align: "center",
          width: "10%",
          scopedSlots: { customRender: "deptcounts" },
        },
        {
          title: "已回执数",
          dataIndex: "yhuizhiCounts",
          ellipsis: true,
          align: "center",
          width: "10%",
          scopedSlots: { customRender: "yhuizhiCounts" },
        },
        {
          title: "待回执数",
          dataIndex: "yhuizhiCounts",
          ellipsis: true,
          align: "center",
          width: "10%",
          scopedSlots: { customRender: "daicounts" },
        },

        {
          title: "创建人",
          dataIndex: "userName",
          ellipsis: true,
          align: "center",
          width: "10%",
        },
        {
          title: "创建时间",
          dataIndex: "createTime",
          ellipsis: true,
          align: "center",
          width: "15%",
        },

        {
          title: "状态",
          dataIndex: "status",
          scopedSlots: { customRender: "status" },
          align: "center",
          width: "10%",
        },
        {
          title: "操作",
          dataIndex: "operation",
          align: "left",
          width: "15%",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("linshi_manage_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getTreeselect();
  },
  methods: {
    onfabuBizLinshiManage(item) {
      var that = this;
      item.arrayDeptIds = JSON.parse(item.deptIds);
      item.arrayDeptNames = JSON.parse(item.deptNames);
      this.$confirm({
        title: "确认发布该督办信息？",
        onOk() {
          return fabuBizLinshiManage(item).then((response) => {
            if (response.code === 200) {
              that.getList();
              that.$message.success("发布成功", 3);
            } else {
              that.getList();
              that.$message.error("发布失败", 3);
            }
          });
        },
        onCancel() {},
      });
    },
    getTreeselect() {
      listDeptTree("0", 3).then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询临时督办列表 */
    getList() {
      this.loading = true;
      listBizLinshiManage(this.queryParam).then((response) => {
        this.bizLinshiManageList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row) {
      if (row.status) {
        return this.selectDictLabel(this.statusOptions, row.status);
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd() {
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizLinshiManageAddForm.handleAdd());
    },
    handleUpdate(record, ids) {
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizLinshiManageEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizLinshiManageIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizLinshiManage(bizLinshiManageIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizLinshiManage(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
